<script>
	import common from './utils/wxApplet/common.js'
	// #ifdef APP-PLUS
	import APPUpdate from '@/uni_modules/zhouWei-APPUpdate/js_sdk/appUpdate';
	// #endif
	export default {
		globalData: {
			spid: ''
		},
		onLaunch: function() {
			console.log('>>>>>>>>>>>>>>>会话开始<<<<<<<<<<<<<<')
			common.getSysmbolInfo();
			// this.updateConfig()
			const launchOptions = uni.getLaunchOptionsSync();
			// console.log(launchOptions);
			const pid = launchOptions.query.spid
			const path = launchOptions.query.path
			if (pid) {
				this.$u.vuex('vuex_SPID', pid)
			}
			if (path) {
				console.log(path);
				if (path.indexOf('page/index/index') >= 0) {
					uni.switchTab({
						url: path
					})
				} else {
					uni.reLaunch({
						url: `/${path}`
					})
				}
			}
			// #ifdef APP-PLUS
			APPUpdate();
			this.pushPromisse()
			// #endif
			// #ifdef MP
			common.wxLogin();
			this.mpUpdateManager()
			// #endif
		},
		onShow: function() {},
		onHide: function() {},
		methods: {
			pushPromisse() {
				const isLogin = this.$store.state.vuex_token;
				const sysmbol = this.$store.state.vuex_sysmbelInfo;
				if (isLogin) {
					const userInfo = this.$store.state.vuex_userInfo
					if (userInfo.cid && userInfo.notice_set.all == 1) {
						uni.onPushMessage((res) => {
							console.log("收到推送消息：", res) //监听推送消息
						})
					} else {
						uni.showModal({
							content: '为了能即时收到消息，请同意本应用向您发送通知',
							cancelText: sysmbol.platform == 'android' ? '同意' : '不同意',
							confirmText: sysmbol.platform == 'android' ? '不同意' : '同意',
							success: (res) => {
								if (sysmbol.platform == 'android') {
									if (res.cancel) {
										this.onPushMessage()
									}
								}
								if (sysmbol.platform == 'ios') {
									if (res.confirm) {
										this.onPushMessage()
									}
								}
							}
						})
					}
				}

			},
			onPushMessage() {
				uni.getPushClientId({
					success: (res) => {
						this.$u.api.cidBind({
							cid: res.cid
						}).then(rep => {

							uni.onPushMessage((push) => {
								// if (push.type == 'receive') {
								// 	uni.createPushMessage(push.data)
								// }
								console.log("收到推送消息：", push) //监听推送消息
							})

						})
						this.$u.api.pushSet({
							open: 1,
							type: 'all'
						})
					},
					fail: (err) => {
						console.log(err);
					}
				})
			},
			mpUpdateManager() {
				const updateManager = uni.getUpdateManager();
				updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					console.log('版本信息', res.hasUpdate);
				});

				updateManager.onUpdateReady(function(res) {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						}
					});

				});

				updateManager.onUpdateFailed(function(res) {
					// 新的版本下载失败
				});
			}
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	@import "common/iconfont.css";

	page {
		background: $u-bg-color;
		color: $u-main-color;
		font-size: 24rpx;
		height: 100%;
		line-height: 1.5;
		min-height: 100%;
	}

	page::-webkit-scrollbar,
	view::-webkit-scrollbar,
	*::-webkit-scrollbar {
		display: none;
	}

	view,
	navigator,
	image,
	text,
	scroll-view,
	swiper,
	swiper-item,
	input,
	textarea,
	button {
		box-sizing: border-box;
	}

	/* 文字避头尾法则 */
	.btw {
		table-layout: fixed;
		word-wrap: break-word;
		word-break: normal;
		text-align: justify;
		text-justify: inter-ideograph;
	}

	.bg-white {
		background: #fff;
	}

	.font-weight {
		font-weight: bold;
	}

	.u-flex-center {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}



	button::after {
		border: none;
	}

	// 指定按钮按下去的样式类
</style>